<?php
namespace app\admin\controller;
use app\admin\controller\Common;

use think\facade\Db;
use think\facade\View;

class Vote extends Common{
	public function votetitle(){
	    $data=request()->get();
		$whe["del_status"]=2;
        if(!empty($data)){
            
        }
        $list=Db::name('vote')->where($whe)->order('id desc')->paginate(20);
        foreach($list as $voteinfo){
            $votestatus="进行中";
            if(time()-$voteinfo["start_time"]<0){
                $votestatus="未开始";
                
            }
            if(time()-$voteinfo["end_time"]>0){
                $votestatus="已截止";
               
            }
            $voteinfo["votestatus"]=$votestatus;
            $votelist[] = $voteinfo;
        }
        if(isset($votelist)){
            View::assign('votelist',$votelist);
        }else{
            View::assign('votelist',$list);
        }
		View::assign("list",$list);
		$number=Db::name('vote')->where($whe)->count();
		View::assign("number",$number);
		foreach($data as $k => $v) {
            View::assign($k, $v);
        }
		return View::fetch("vote_title");
	}
	
	public function votetitleAdd(){
	    if(request()->isPost()){
	        $data=request()->post();
	        //var_dump($data);die;
	        $data["start_time"]=strtotime($data["start_time"]);
	        $data["end_time"]=strtotime($data["end_time"]);
	        $res=Db::table("vote")->insert($data);
	        if($res){
	            return "添加成功";
	        }else{
	            return "添加失败";
	        }
	    }else{
	        return View::fetch("vote_title_add");
	    }
	}
	
	public function votetitleEdit(){
	    if(request()->isPost()){
	        $data=request()->post();
	        $data["start_time"]=strtotime($data["start_time"]);
	        $data["end_time"]=strtotime($data["end_time"]);
	        $res=Db::table("vote")->update($data);
	        if($res){
	            return "修改成功";
	        }else{
	            return "修改失败";
	        }
	    }else{
	        $id=request()->get("id");
	        $info=Db::table("vote")->where("id",$id)->find();
	        View::assign("info",$info);
	        return View::fetch("vote_title_edit");
	    }
	}
	public function votetitleCon(){
	    if(request()->isPost()){
	        $data=request()->post();
	        $data["start_time"]=strtotime($data["start_time"]);
	        $data["end_time"]=strtotime($data["end_time"]);
	        $res=Db::table("vote")->update($data);
	        if($res){
	            return "修改成功";
	        }else{
	            return "修改失败";
	        }
	    }else{
	        $id=request()->get("id");
	        $info=Db::table("vote")->where("id",$id)->find();
	        View::assign("info",$info);
	        return View::fetch("vote_title_con");
	    }
	}
	public function votetitleDel(){
	    $id=request()->get("id");
	    $info=Db::table("vote")->where("del_status",2)->where("id",$id)->find();
	    if(empty($info)){
	        $this->error("改数据不存在");
	    }
	    $res=Db::table("vote")->where("id",$id)->update(["del_status"=>"1","del_name"=>session('name')]);
	    if($res){
	        //删除投票选项
	        Db::table("vote_select")->where("vote_id",$id)->update(["del_status"=>"1"]);
	        //删除投票日志
	        Db::table("vote_log")->where("vote_id",$id)->update(["del_status"=>"1"]);
	        $this->success("删除成功");
	    }else{
	        $this->error("删除失败");
	    }
	    echo $id;
	}
	
	public function voteselect(){
	    $data=request()->get();
		$whe["del_status"]=2;
        if(!empty($data)){
            
        }
        $list=Db::name('vote_select')->where($whe)->order('id desc')->paginate(10);
        foreach($list as $info){
            $num=Db::table("vote_select")->where("del_status",2)->where("vote_id",$info["vote_id"])->sum("num");
            $selectnum=Db::table("vote_select")->where("del_status",2)->where("vote_id",$info["vote_id"])->count();
            $per=1/$selectnum;
            if($num!="0"){
                $per=$info["num"]/$num;
            }
            $info["per"]=round($per*100).'%';
           
            $selectlist[] = $info;
        }
        
        if(isset($selectlist)){
            View::assign('selectlist',$selectlist);
        }else{
            View::assign('selectlist',$list);
        }
		View::assign("list",$list);
		$number=Db::name('vote_select')->where($whe)->count();
		View::assign("number",$number);
		foreach($data as $k => $v) {
            View::assign($k, $v);
        }
	    return View::fetch("vote_select");
	}
	public function voteselectAdd(){
	    if(request()->isPost()){
	        $data=request()->post();
	        
	        $res=Db::table("vote_select")->insert($data);
	        if($res){
	            return "添加成功";
	        }else{
	            return "添加失败";
	        }
	    }else{
	      
	        $voteinfo=Db::table("vote")->order('id desc')->limit("1")->find();
	        
	        View::assign("voteinfo",$voteinfo);
	        return View::fetch("vote_select_add");
	    }
	}
	
	public function votelog(){
	    $data=request()->get();
		$whe["del_status"]=2;
        if(!empty($data)){
            
        }
        $export = json_encode($whe);
        $export = str_replace("\"","|",$export);
        View::assign('export',$export);
        $list=Db::name('vote_log')->where($whe)->order('id desc')->paginate(20);
        foreach($list as $info){
            $voteinfo=Db::table('vote')->where("id",$info["vote_id"])->find();
            
            $selectinfo=Db::table('vote_select')->where("id",$info["vote_select_id"])->find();
            $info["vote_id"]=$voteinfo["title"];
            $info["vote_select_id"]=$selectinfo["con"];
            $loglist[] = $info;
        }
        
        if(isset($loglist)){
            View::assign('loglist',$loglist);
        }else{
            View::assign('loglist',$list);
        }
		View::assign("list",$list);
		$number=Db::name('vote_log')->where($whe)->count();
		View::assign("number",$number);
		foreach($data as $k => $v) {
            View::assign($k, $v);
        }
	    return View::fetch("vote_log");
	}
	
	public  function votelogExport(){
        //获取账单数据
        $export = request()->param('export');
        $export = str_replace("|","\"",$export);
        $data = json_decode($export,true);
        
        $whe=$data;
        //多条件查询地库车位信息
        $list=Db::name('vote_log')->where($whe)->order('id desc')->select();
        $number = Db::name('vote_log')->where($whe)->count();
        
        if($number=="0"){
            return "没有数据";
        }
    

        //设置表头
        //引入PHPExcel类
        vendor('PHPExcel.PHPExcel.Reader.Excel2007');
        $objPHPExcel = new \PHPExcel();
        //设置表头
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '投票主题')
            ->setCellValue('B1', '投票选项')
            ->setCellValue('C1', '姓名')
            ->setCellValue('D1', '手机')
            ->setCellValue('D1', '房号')
            ->setCellValue('E1', '投票时间');

        $i=2;  //定义一个i变量，目的是在循环输出数据是控制行数
         //计算有多少条数据
        for ($i = 2; $i <= $number+1; $i++) {
            
           
            $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $list[$i-2]["title"]);
            $vote_select_id=$list[$i-2]['vote_select_id'];
            $selectinfo=Db::table("vote_select")->where("id",$vote_select_id)->find();
            $con=$selectinfo["con"];
            $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $con);//
            $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $list[$i-2]['name']);
            $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $list[$i-2]['phone']);
            $address=$list[$i-2]['building']."-".$list[$i-2]['unit']."-".$list[$i-2]['room'];
            $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $address);
            $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $list[$i-2]['create_time']);
           
           
        }
        $objPHPExcel->getActiveSheet()->setTitle("投票记录");      //设置sheet的名称
        $objPHPExcel->setActiveSheetIndex(0);                   //设置sheet的起始位置
        header('Content-Disposition: attachment;filename="投票记录'.date('YmdHi').'.xls"');
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        $PHPWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel,"Excel2007"); //通过PHPExcel_IOFactory的写函数将上面数据写出来
        $PHPWriter->save("php://output"); //表示在$path路径下面生成demo.xlsx文件
        
    }
}
